{include file="common/top" /}
<style>
html{
	background: #ffffff;
}
</style>
<form class="layui-form " action="" style="    background: #fff;">
	<div class="layui-form-item">
		<label class="layui-form-label">充值编号：</label>
		<div class="layui-col-xs4">
			<input type="text" name="code" value="{$num}" readonly="readonly" autocomplete="off" class="layui-input">
		</div>
		<label class="layui-form-label">*充值时间：</label>
		<div class="layui-col-xs4">
			<input type="text" name="create_time" id="date" lay-verify="datetime" placeholder="yyyy-MM-dd H:i:s" autocomplete="off" class="layui-input">
		</div>
		
	</div>
	<div class="layui-form-item layui-col-space10">
		<label class="layui-form-label">*账户ID：</label>
		<div class="layui-col-xs4">
			<select name="op_account_id" lay-verify="required" lay-search='' lay-filter="channel"  autocomplete="off" >
				<option value="">直接选择或搜索选择</option>
				{volist name="OpAccount_list" id='acc'}
					<option value="{$acc.id}" >{$acc.name_id}</option>
				{/volist}
	        </select>
		</div>
		<label class="layui-form-label">*端口：</label>
		<div class="layui-col-xs4">
			<input type="text" id="dk" readonly="readonly"   autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item layui-col-space10">
		<label class="layui-form-label">*客户名称：</label>
		<div class="layui-col-xs4">
			<input type="text" id="kh" readonly="readonly" lay-verify="required" placeholder="客户名称" autocomplete="off" class="layui-input">
		</div>
		<label class="layui-form-label">*客户余额：</label>
		<div class="layui-col-xs4">
			<input type="text"  id="ye" readonly="readonly" lay-verify="required" placeholder="余额(元)" autocomplete="off" class="layui-input">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label ">*客户返点：</label>
		<div class="layui-col-xs4">
			<input type="text" name="rebate" lay-verify="required" autocomplete="off" placeholder="返点百分比" class="layui-input fd">
		</div>
		<label class="layui-form-label">*渠道返点：</label>
		<div class="layui-col-xs4">
			<input type="text" name="channel_rebate" lay-verify="required"  autocomplete="off" placeholder="服务费百分比" class="layui-input fwf">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">媒介：</label>
		<div class="layui-col-xs4">
			<select name="media_id" lay-verify="required" lay-search=""  autocomplete="off">
				<option value="">直接选择或搜索选择</option>
				{volist name="user_list" id='user'}
					<option value="{$user.id}" {eq name=":session('uid')" value="$user.id"}selected{/eq}>{$user.username}</option>
				{/volist}
	        </select>
		</div>
		<label class="layui-form-label">*销售返点：</label>
		<div class="layui-col-xs4">
			<input type="text" name="sell_rebates" lay-verify="required" autocomplete="off" placeholder="销售返点百分比" class="layui-input fd">
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">*账户币：</label>
		<div class="layui-col-xs4">
			<input type="text" name="currency" placeholder="请输入账户币"  autocomplete="off"  class="layui-input " oninput="get_zhb(this)">
		</div>
		<label class="layui-form-label">*充值金额：</label>
		<div class="layui-col-xs4">
			<input type="text" name='amount'  autocomplete="off"  class="layui-input zhb" >
		</div>
	</div>
	
	
	<div class="layui-form-item">
		<label class="layui-form-label">备注：</label>
		<div class="layui-col-xs9">
			<textarea name="note" autocomplete="off" placeholder="请输入详细备注" class="layui-textarea"></textarea>
		</div>
	</div>
	<hr />
	<div class="layui-form-item">
		<div class="layui-input-block">
			<input type="hidden" id="customer" name="customer_id" value=""  >
			<input type="hidden" id="items" name="items_id" value=""  >
			<input type="hidden" id="channel_id" name="channel_id" value=""  >
			<button class="layui-btn"  lay-submit="" lay-filter="demo1">提交</button>
		</div>
	</div>
	
</form>
{include file="common/footer" /}
<script>
	var t = new Date().getTime();
	layui.use(['form', 'layedit', 'laydate'], function(){
		  var form = layui.form ,layer = layui.layer,laydate = layui.laydate;
		  laydate.render({
			    elem: '#date', //指定元素
			    type: 'datetime',
			    value: new Date(t)
			  });
		  //监听提交
		  form.on('submit(demo1)', function(data){
			var articleFrom = data.field;
			if(is_yue()){
				layer.confirm('客户余额不足，确认要充值吗', function(index){
					get_add(articleFrom);
				});  
			}else{
				get_add(articleFrom);
			}
			
			return false;
		  });
		  function get_add(articleFrom){
			layer.load(1, { shade : [ 0.5, '#676a6c' ] }); 
			$.post("{:url('acc_recharges')}?t=json",articleFrom,function(data){
				layer.closeAll();
				if (data['code'] == 1) { 
					var index1 = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
					parent.layer.close(index1); //再执行关闭 
					//父页面数据 ，重新加载
					parent.get_acc(data['msg']);

				} else {
					layer.msg(data['msg'], { icon : 5 });
				}
			});
		  }
			//判断余额是否足够
			function is_yue(){
				var yue = $("#ye").val();
				var y = $("input[name=amount]").val();
				if(y*1 > yue*1){
					return true;
				}else{
					return false;
				}
			}  
		
			//下拉菜单事件 渠道
			form.on('select(channel)', function (data) {
				layer.load(1, { shade : [ 0.5, '#676a6c' ] }); 
				$.post("{:url('get_OpAccount_info')}?t=json",{acc_id:data.value},function(da){
					layer.closeAll();
					  if(da['code'] == 1){
						  	da = da.data;
							
							$("#customer").val(da.cu_id);
							$("#xm").val(da.item_name);
							$("#items").val(da.item_id);
							$("#kh").val(da.cu_name);
							$("#ye").val(da.prepare_money);
							$("input[name=rebate]").val(da.rebates);
							$("input[name=service_pay]").val(da.service);
							$("input[name=channel_rebate]").val(da.channel_rebate);
							$("#channel_id").val(da.channel_id);
							$("#dk").val(da.channel_name);
							form.render();
							
					  }else{
						  $("#accselect").empty();
						  get_null();
						  form.render("select");
						  layer.msg(da['msg'], { icon : 5 });
					  }
				})
				
			})
		
			function get_null(){
				
		 		$("#customer").val('');
		 		$("#xm").val('');
		 		$("#items").val('');
		 		$("#kh").val('');
		 		$("#ye").val('');
		 		$("input[name=rebate]").val('');
		 		$("input[name=service_pay]").val('');
		 		$("input[name=channel_rebate]").val('');
				 $("#channel_id").val('');
				 $("#dk").val('');
			}
		});
	function get_zhb(obj){
		var fd = $('.fd').val();
		var money = $(obj).val();
		var tt=/^[0-9]+.?[0-9]*$/;
		
		if(!tt.test(money) || money <= 0){
			$('.zhb').val('');
			layer.msg('账户币必须大于0', { icon : 5 });
			return false;
		}
		var m;
		var fd_bl;
		if(fd==0){
			m = money;
		}else if(fd>0){
			fd_bl = (100+parseInt(fd))/100;
			m = (money/fd_bl).toFixed(2);
			
		}else if(fd<0){
			fd_bl = Math.abs(parseInt(fd));
			console.log(fd_bl);
			fd_bl = (100+fd_bl)/100;
			m = (money*fd_bl).toFixed(2);
		}
		
		$('.zhb').val(m);
		
		
	}
	
	
</script>
